package org.example.car.system.config;

import lombok.Getter;

import java.util.concurrent.*;

/**
 * @author Lin
 * @createDate 2024-06-26 19:48:21
 */
public class ThreadPoolUtils {
    @Getter
    private static final ThreadPoolExecutor threadPoolExecutor;

    static {
        //获取cpu核心数
        int i = Runtime.getRuntime().availableProcessors();
        //核心线程数
        int corePoolSize = i * 2;
        //最大线程数
        int maximumPoolSize = i * 2;
        //线程无引用存活时间
        long keepAliveTime = 60;
        //时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        //任务队列，接收一个整型的参数，这个整型参数指的是队列的长度，
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(i * 2 * 10);
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        //拒绝执行处理器
        RejectedExecutionHandler handler = new ThreadPoolExecutor.CallerRunsPolicy();
        //创建线程池
        threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }
}



